
var projectPage = 1;
var projectCount = 0;

var firstSite = "https://www.atu360.cn/api/phone/index";
var secondSite = "https://www.atu360.cn/api/phone/findProjectClass?level=0&limit=10";


function loadXMLDoc() {
    loadXMLDoc1();
    loadXMLDoc2();
}

function loadXMLDoc1() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp = new XMLHttpRequest();
    }
    else {
        // IE6, IE5 浏览器执行代码
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var xmlData = JSON.parse(this.responseText);
            //设置轮播
            carouselPicture(xmlData);
        }
    };
    xmlhttp.open("GET", firstSite, true);
    xmlhttp.send();
}

function loadXMLDoc2() {
    var xmlhttp;

    if (window.XMLHttpRequest) {
        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp = new XMLHttpRequest();
    }
    else {
        // IE6, IE5 浏览器执行代码
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var xmlData = JSON.parse(this.responseText);
            //列出产品
            listProject(xmlData);
        }
    };
    xmlhttp.open("GET", secondSite + "&page=" + projectPage, true);
    xmlhttp.send();
}
//写入轮播链接
function carouselPicture(carouselData) {
    for (var i = 0; i < 5; i++) {
        document.getElementById("ADUrl_" + i).href = carouselData.Ad[i].url;
        document.getElementById("ADImage_" + i).src = carouselData.Ad[i].image;
    }
}
//列出产品
function listProject(projectData) {
    var projectPages = projectData.pages;
    if (projectPage <= projectPages) {
        for (i = 0; i < projectData.limit; i++ , projectCount++) {
            insertProject(projectCount, projectData);
        }
        projectPage += 1;
    }
    else{
        //移除按钮
        var parent = document.getElementById("leftField");
        var child = document.getElementById("loadMore");
        parent.removeChild(child);
    }
}

//创建新项目
function insertProject(projectCount, projectData) {
    newMedia(projectCount);
    newMediaBody(projectCount);
    newA(projectCount, projectData);
    newTitle(projectCount, projectData);
    newIntroduce(projectCount, projectData);
    newProjectId(projectCount,projectData);
    newImage(projectCount, projectData);
}

//创建 media 容器 div
function newMedia(projectCount) {
    var newMedia = document.createElement("div");
    newMedia.id = "newMedia_" + projectCount;
    newMedia.className = "media border-top p-3";
    newMedia.style.marginBottom = "5px";
    document.getElementById("showProject").appendChild(newMedia);
}

//创建 mediabody 容器 div
function newMediaBody(projectCount) {
    var newMediaBody = document.createElement("div");
    newMediaBody.id = "newMediaBody_" + projectCount;
    newMediaBody.className = "meidia-body";
    newMediaBody.style.width = "80%";
    document.getElementById("newMedia_" + projectCount).appendChild(newMediaBody);
}

//创建项目链接
function newA(projectCount, projectData) {
    var newA = document.createElement("a");
    newA.id = "newA_" + projectCount;
    newA.href = "#";
    newA.style.color = "black";
    newA.style.textDecoration = "none";
    document.getElementById("newMediaBody_" + projectCount).appendChild(newA);
    document.getElementById("newA_" + projectCount).href = projectData.docs[i].url;
}

//创建项目标题
function newTitle(projectCount, projectData) {
    var newTitle = document.createElement("h4");
    newTitle.id = "newTitle_" + projectCount;
    document.getElementById("newA_" + projectCount).appendChild(newTitle);
    document.getElementById("newTitle_" + projectCount).innerHTML = projectData.docs[i].titleName;
}

//创建项目简介
function newIntroduce(projectCount, projectData) {
    var newIntroduce = document.createElement("p");
    newIntroduce.id = "newIntroduce_" + projectCount;
    document.getElementById("newMediaBody_" + projectCount).appendChild(newIntroduce);
    document.getElementById("newIntroduce_" + projectCount).innerHTML = projectData.docs[i].intro;
}

//加入方案 ID
function newProjectId(projectCount,projectData) {
    var newProjectId = document.createElement("span");
    newProjectId.id = "newProjectId_" + projectCount;
    document.getElementById("newMediaBody_" + projectCount).appendChild(newProjectId);
    document.getElementById("newProjectId_" + projectCount).innerHTML = projectData.docs[i].project_id;
}

function newProjectLike(projectCount,projectData) {
    var newProjectLike = document.createElement("a");
    newProjectLike.id = "newProjectLike_" + projectCount;
    newProjectLike.type = "button";
    newProjectLike.className = "btn";
    newProjectLike.style.display = "inline";
    document.getElementById("newMediaBody_" + projectCount).appendChild(newProjectLike);
    document.getElementById("newProject_" + projectCount).insertAdjacentText = "点赞";
}

//创建项目 Icon
function newImage(projectCount, projectData) {
    var newImage = document.createElement("img");
    newImage.id = "newImage_" + projectCount;
    newImage.href = "#";
    newImage.alt = "Block Graph";
    newImage.className = "ml-3 mt-3";
    newImage.style.width = "200px";
    document.getElementById("newMedia_" + projectCount).appendChild(newImage);
    document.getElementById("newImage_" + projectCount).src = projectData.docs[i].icon;
}

// function newprojectClassify(projectCount,projectData) {
//     var newprojectClassify = document.createElement("a");
//     newprojectClassify.id = "newprojectClassify_" +projectData;
//     newprojectClassify.href = "#";
//     newprojectClassify.style.color = "lightslategray";
//     newprojectClassify.style.display = "inline-block";
// }

// function newLabelContainer(i) {
//     var newLabelContainer = document.createElement("div");
//     newLabelContainer.id = "newLabelContainer_" + i;
//     document.getElementById("hotLabel").appendChild(newLabelContainer);
// }

// function newLabel(i) {
//     var newLabel = document.createElement("a");
//     newLabel.id = "newLabel_" + i;
//     newLabel.className = "btn border rounded";
//     newLabel.target = "_blank";
//     newLabel.href = "#";
//     newLabel.color = "black";
//     newLabel.style.backgroundColor = "darkgray";
//     newLabel.style.marginRight = "10px";
//     document.getElementById("newLabelContainer_" + i).appendChild(newLabel);
// }